package com.liu.student.mapper;

import com.liu.student.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Select("SELECT * FROM student")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "studentNumber", column = "student_number"),
        @Result(property = "name", column = "name"),
        @Result(property = "age", column = "age"),
        @Result(property = "gender", column = "gender"),
        @Result(property = "major", column = "major"),
        @Result(property = "className", column = "class_name"),
        @Result(property = "enrollmentDate", column = "enrollment_date")
    })
    List<Student> findAll();

    @Select("SELECT * FROM student WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "studentNumber", column = "student_number"),
        @Result(property = "name", column = "name"),
        @Result(property = "age", column = "age"),
        @Result(property = "gender", column = "gender"),
        @Result(property = "major", column = "major"),
        @Result(property = "className", column = "class_name"),
        @Result(property = "enrollmentDate", column = "enrollment_date")
    })
    Student findById(Long id);

    @Select("SELECT s.* FROM student s " +
            "INNER JOIN user u ON s.user_id = u.id " +
            "WHERE u.username = #{username}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "studentNumber", column = "student_number"),
        @Result(property = "name", column = "name"),
        @Result(property = "age", column = "age"),
        @Result(property = "gender", column = "gender"),
        @Result(property = "major", column = "major"),
        @Result(property = "className", column = "class_name"),
        @Result(property = "enrollmentDate", column = "enrollment_date")
    })
    Student findByUsername(String username);

    @Insert("INSERT INTO student (user_id, student_number, name, age, gender, major, class_name, enrollment_date) " +
            "VALUES (#{userId}, #{studentNumber}, #{name}, #{age}, #{gender}, #{major}, #{className}, #{enrollmentDate})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Student student);

    @Update("UPDATE student SET name = #{name}, age = #{age}, gender = #{gender}, " +
            "major = #{major}, class_name = #{className} WHERE id = #{id}")
    int update(Student student);

    @Delete("DELETE FROM student WHERE id = #{id}")
    int deleteById(Long id);

    @Select("SELECT COUNT(*) FROM student")
    long count();
}


